package com.aiti.lulian.utils;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.aiti.base.core.base.dto.PageDto;
import com.aiti.base.core.exception.BusinessException;
import com.aiti.lulian.gxmh.dto.BasePageParam;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Base64;
import java.util.List;

/**
 * @description: BusinessUtil
 * @auth fcb
 * @date 2025-03-08 09:33
 **/
public class BusinessUtil {

    private BusinessUtil() {
    }

    public static void checkPageParam(PageDto pageDto) {
        if (pageDto.getPageNum() == null || pageDto.getPageNum() < 1) {
            throw new BusinessException("分页查询参数错误");
        }
        if (pageDto.getPageSize() == null || pageDto.getPageSize() < 1) {
            throw new BusinessException("分页查询参数错误");
        }
    }

    public static void checkPageParam(BasePageParam pageDto) {
        if (pageDto.getPageNum() == null || pageDto.getPageNum() < 1) {
            throw new BusinessException("分页查询参数错误");
        }
        if (pageDto.getPageSize() == null || pageDto.getPageSize() < 1) {
            throw new BusinessException("分页查询参数错误");
        }
    }

    /**
     * 检查对象的所有属性是否均为null或空字符串（不检查继承属性）
     * @param object 待检查的对象
     * @return 若所有属性均为空，返回true；否则返回false
     */
    public static boolean allPropertiesEmpty(Object object){
        try {
            if (object == null) return true;

            Class<?> clazz = object.getClass();
            Field[] fields = clazz.getDeclaredFields(); // 仅获取本类声明的属性

            for (Field field : fields) {
                field.setAccessible(true); // 允许访问私有属性
                Object value = field.get(object);
                // 情况1：属性值为null -> 跳过
                if (value == null) {
                    continue;
                }
                // 情况2：属性为字符串类型且内容为空（包括纯空格）
                if (value instanceof String) {
                    if (!StrUtil.isBlank((String) value)) {
                        return false; // 非空字符串
                    }
                } else if (value instanceof List) {
                    List v = (List) value;
                    if (!v.isEmpty()) {
                        return false;
                    }
                }
                // 情况3：非字符串的其他类型属性（如Integer、自定义类等）
                else {
                    return false; // 非空对象
                }
            }
            return true;
        }catch (Exception e) {
            return false;
        }
    }

    public static String bufferedImageToBase64(BufferedImage image, String formatName) {
        try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
            ImageIO.write(image, formatName, baos);
            baos.flush();
            byte[] imageBytes = baos.toByteArray();
            return Base64.getEncoder().encodeToString(imageBytes);
        } catch (IOException e) {
            throw new RuntimeException("Error converting BufferedImage to Base64", e);
        }
    }
}
